﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Control_menu_right_right_menu : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["KhachHang"] == null)
        {
            ttc.Visible = true;
            txtU.Visible = true;
            txtpass.Visible = true;
            mk.Visible = true;
            Button1.Visible = true;
            Button2.Visible = false;
            Label2.Visible = true;
        }
        else {
            this.hello.Visible = true;
            string ss = Session["KhachHang"].ToString();
            this.hello.Text = "Xin chào : " +ss ;
            this.Button2.Visible = true;
            ttc.Visible = false;
            txtU.Visible = false;
            txtpass.Visible = false;
            mk.Visible = false;
            Button1.Visible = false;
            Label2.Visible = false;
            Label1.Visible = false;
        }
    }
    private Entities en = new Entities();
    private DataAccess da = new DataAccess();
    protected void Button1_Click(object sender, EventArgs e)
    {
        string uh = txtU.Text;
        string ph = en.EncodePassword(txtpass.Text.ToString(),"");
        if (checklogin(uh,ph) == true)
        {
            Session["KhachHang"] = uh;
            this.hello.Visible = true;
            string ss = Session["KhachHang"].ToString();
            this.hello.Text = "Xin chào : " + ss;
            this.Button2.Visible = true;
            ttc.Visible = false;
            txtU.Visible = false;
            txtpass.Visible = false;
            mk.Visible = false;
            Button1.Visible = false;
            Label2.Visible = false;
        }
        else
        {            
            Label1.Text = "Tên truy cập hoặc mật khẩu không đúng!";
        }
    }
    private bool checklogin(string u, string p)
    {
        SqlConnection sconn = da.SqlConn();
        sconn.Open();
        SqlCommand cmd = new SqlCommand("select * from tb_KhachHang where TenTruyCapKH= '" + u + "' and MatKhauKH ='" + p + "'", sconn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            sconn.Close();
            return true;
        }
        sconn.Close();
        return false;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        ttc.Visible = true;
        txtU.Visible = true;
        txtpass.Visible = true;
        mk.Visible = true;
        Button1.Visible = true;
        Button2.Visible = false;
        hello.Visible = false;
        Label2.Visible = true;
        Response.Redirect("Home.aspx");
    }
}
